package cn.changeforyou.web.blog.controller;

import cn.changeforyou.web.blog.po.User;
import cn.changeforyou.web.blog.service.UseService;
import cn.changeforyou.web.blog.vo.R;
import cn.changeforyou.web.nutz.common.security.SecurityConfig;
import java.util.List;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

/**
 * @author zhyu
 */
@At("/user")
@IocBean
@Ok("json:full")
public class UserController {

    @Inject
    private Dao dao;
    @Inject
    private UseService useService;

    @Ok("raw")
    @At
    public long count() {
        return dao.count(User.class);
    }

    @At
    public NutMap query(@Param("..")Pager pager) {
        List<User> users = dao.query(User.class, Cnd.orderBy().desc("id"), pager);
        pager.setRecordCount(dao.count(User.class));
        return new NutMap("ok", true).setv("data", new QueryResult(users, pager));
    }

    @At
    @POST
    @Ok("json:{locked:'password', ignoreNull:true}")
    public R<User> login(@Valid @Param("..") User user) {
        return R.suc(useService.login(user));
    }

    @At
    @GET
    public R logout() {
        return R.suc();
    }

}
